package com.ihighest.app.infox.activity;

import io.rong.imkit.RongIM;

import org.json.JSONException;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.widget.TextView;

import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiParam;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.common.kit.SharedPreferencesKit;
import com.ihighest.app.common.kit.ToastKit;
import com.ihighest.app.infox.Const;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.myview.ActivityTitle;

public class EventDetailActivity extends BaseActivity {
	private ActivityTitle title;
	private TextView eventTitle, publishTime;
	private String eventState, eventId;
	private WebView eventContent;
	private String mEventTitle;
	private String isJoin;// 是否参加
	private String gId;// 群组id
	private String rId;// 融云，群组id
	private String actTitle;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.infox_activity_eventdetail);
		String action = getIntent().getAction();

		if (Intent.ACTION_VIEW.equals(action)) {
			Uri uri = getIntent().getData();
			if (uri != null) {
				eventId = uri.getQueryParameter("id");
			}
		} else {
			eventState = getIntent().getStringExtra("state");
			eventId = getIntent().getStringExtra("id");
			isJoin = getIntent().getStringExtra("isJoin");
			gId = getIntent().getStringExtra("gid");
			rId = getIntent().getStringExtra("rid");
			actTitle = getIntent().getStringExtra("title");
		}
		initView();
	}

	/**
	 * 初始化绑定
	 * 
	 * 
	 * @author lkx Created at 2015-10-26 下午8:30:19
	 */
	private void initView() {
		title = (ActivityTitle) findViewById(R.id.eventDetailActivityTitle);
		title.setTitle("活动详情");
		title.setViewVersible(title.titleBack, title.rightText);
		title.setViewGone(title.titleMsg, title.titleRefresh);
		title.setRightTextViewText("参加");

		eventTitle = (TextView) findViewById(R.id.eventDetailTitle);
		publishTime = (TextView) findViewById(R.id.publishTime);
		eventContent = (WebView) findViewById(R.id.enentContent);
		if (!TextUtils.isEmpty(eventState) && eventState.equals("Finished")) {
			title.setRightTextViewText("已结束");
		} else {
			if (!TextUtils.isEmpty(eventState) && isJoin.equals("no")) {
				title.setRightTextViewText("参加");
			} else if (!TextUtils.isEmpty(eventState) && isJoin.equals("yes")) {
				title.setRightTextViewText("参与讨论");
			}
		}

		eventContent.getSettings().setJavaScriptEnabled(true);
		eventContent.getSettings().setBuiltInZoomControls(false);
		eventContent.getSettings().setSupportZoom(false);
		eventContent.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
		title.rightText.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (title.rightText.getText().toString().equals("参加")) {
					if (TextUtils.isEmpty(SharedPreferencesKit.getString(EventDetailActivity.this, Const.SP_KEY_USER_ID))) {
						ToastKit.showShort(EventDetailActivity.this, Const.UN_LOGIN_ALERT);
					} else {
						commitJoinIn();
					}

				} else if (title.rightText.getText().toString().equals("参与讨论")) {
					Const.IM_EXTRA = "group_activity:" + eventId + ":"
							+ SharedPreferencesKit.getString(getApplicationContext(), Const.SP_KEY_USER_MID);
					RongIM.getInstance().startGroupChat(EventDetailActivity.this, rId, actTitle + " 活动讨论组");
				}

			}

		});
		initData(eventId);
	}

	/**
	 * 提交参加活动
	 * 
	 * 
	 * @author lkx Created at 2015-10-26 下午9:27:01
	 */
	private void commitJoinIn() {
		Api.post("activity/join", ApiParam.create().addParam("id", mEventTitle), new ApiCallback(EventDetailActivity.this) {

			@Override
			public void handleSuccess(ApiResult result) {
				if (result.getCode() == 100) {
					title.setRightTextViewText("参与讨论");
					ToastKit.showShort(mContext, "已成功参与");
				} else {
					if (!TextUtils.isEmpty(result.getMsg())) {
						ToastKit.showShort(mContext, result.getMsg());
					} else {
						ToastKit.showShort(mContext, "参加活动失败");
					}
				}
			}

			@Override
			public void handleFailure(Exception e, String msg) {
			}
		}, EventDetailActivity.this);

	}

	private void initData(String eventID) {
		String url = "activity/" + eventID + ".json";
		Api.data(url, new ApiCallback(EventDetailActivity.this) {

			@Override
			public void handleSuccess(ApiResult result) {
				try {
					publishTime.setText(AppKit.timeToBeforeStr(result.getData().getLong("time")));
					eventTitle.setText(result.getData().getString("title"));
					eventContent.loadDataWithBaseURL(null, result.getData().getString("content"), "text/html", "utf-8", null);
					mEventTitle = result.getData().getString("id");
				} catch (JSONException e) {
					Log.e(TAG, e.getMessage(), e);
				}

			}

			@Override
			public void handleFailure(Exception e, String msg) {
			}
		}, EventDetailActivity.this);

	}

}
